﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PathologyDAL.Modle;
using PathologyDAL.Service;
using static System.Net.Mime.MediaTypeNames;

namespace PathologyBLL.Service
{
    public class TumourManageServices
    {
        public async Task<List<Tumour>> LoadAsync(string SearchParm)
        {
            TumourServices tumourService = new TumourServices();

            return await tumourService.FindTumourWithFilterStringAsync(SearchParm);
        }

   



        public async Task<CheckResult> AddAsync(Tumour tumour)
        {
            TumourServices tumourService = new TumourServices();

            CheckResult result = new CheckResult();
            result.Success = await CheckTumourAsync(tumour);
            if (result.Success)
            {

                int res = await tumourService.AddTumourAsync(tumour);
                if (res > 0)
                {
                    return result;
                }
                else
                {
                    result.Success = false;
                    result.Message = "写入数据错误！";
                    return result;
                }
            }
            else
            {
                return result;
            }


        }

        public async Task<string> AddTumourAsync(string names)
        {
            TumourServices tumourService = new TumourServices();


            var list = names.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

            if (list.Length == 1)
            {
                int successNumber = await tumourService.AddTumourAsync(new Tumour { Name = names.Trim() });
                if (successNumber == 1)
                {
                    return $"【{names}】添加成功";
                }
                else
                {
                    return $"【{names}】添加失败";
                }
            }
            else
            {
                int successNumber = await tumourService.AddTumorsAsync(list.ToList());
                if (successNumber == list.Length)
                {
                    return $"【{names}】添加成功";
                }
                else
                {
                    return $"【{names}】添加失败";
                }
            }



        }

        public async Task<bool> CheckTumourAsync(Tumour tumour)
        {

            TumourServices tumourService = new TumourServices();
            return await tumourService.CheckNameExistAsync(tumour.Name);


        }
        public async Task<string> CheckTumourAsync(string text)
        {
            TumourServices tumourService1 = new TumourServices();

            var list = text.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList();

            var listexit = await tumourService1.CheckNameExistAsync(list);
            StringBuilder stringBuilder = new StringBuilder();
            foreach (var tumber in listexit)
            {
                stringBuilder.Append(tumber.Name + " ");
            }

            return stringBuilder.ToString();



        }



        public async Task<CheckResult> DeleteAsync(int id)
        {
            TumourServices tumourService = new TumourServices();
            CheckResult checkResult = new CheckResult();
            var res = await tumourService.DeleteAsync(id);

            checkResult.Success = res > 0;

            return checkResult;

        }




        public async Task<CheckResult> UpdateNameAsync(Tumour selectTumour, string value)
        {
            CheckResult checkResult = new CheckResult();
            TumourServices tumourService = new TumourServices();
            selectTumour.Name = value;
            var existRes = await tumourService.CheckNameExistAsync(value);
            if (existRes)
            {
                checkResult.Success = false;
                checkResult.Message = $"[{value}]已经存在";
            }
            else
            {
                int resUpdate = await tumourService.UpdateNameAsync(selectTumour);
                checkResult.Success = resUpdate > 0;
                checkResult.Message = "修改成功!";
            }
            //如果存在已经删除的，将已经删除的那条记录直接删除

            return checkResult;
        }


        public async Task<Tumour> LoadTumourInformationAnsy(Tumour tumour)
        {



            //ConditionServices conditionServices = new ConditionServices();
            //if (tumour.BloodVessel > 0)
            //{
            //    var bv = await conditionServices.GetByID(tumour.BloodVessel);

            //    tumour.BloodVesselName = bv;



            //}
            //if (tumour.CellMorphology > 0)
            //{
            //    var bv = await conditionServices.GetByID(tumour.CellMorphology);

            //    tumour.CellMorphologyName = bv;


            //}
            //if (tumour.Immunohistochemistry > 0)
            //{
            //    var bv = await conditionServices.GetByID(tumour.Immunohistochemistry);

            //    tumour.ImmunohistochemistryName = bv;


            //}
            //if (tumour.Interstitium > 0)
            //{
            //    var bv = await conditionServices.GetByID(tumour.Interstitium);

            //    tumour.InterstitiumName = bv;


            //}
            //if (tumour.OrganizationalStructure > 0)
            //{
            //    var bv = await conditionServices.GetByID(tumour.OrganizationalStructure);

            //    tumour.OrganizationalStructureName = bv;


            //}


            return tumour;
        }

        public async Task<List<TumourConditionDisPlay>> LoadTumourConditionByTumourAsync(Tumour tumour)
        {
            TumourServices tumourService = new TumourServices();
            return await tumourService.LoadTumourConditionByTumourAsync(tumour);
        }

        public async Task<bool> CheckTumourConditionsExistAsync(Tumour tumour, FilterCondition selectCondition)
        {
            TumourServices tumourService = new TumourServices();
            return await tumourService.CheckTumourConditionsExistAsync(tumour, selectCondition);
        }

        public async Task<int> AddTumourConditionAsync(Tumour tumour, FilterCondition selectCondition, int possibleInt)
        {
            TumourServices tumourService = new TumourServices();
            return await tumourService.AddTumourConditionAsync(tumour, selectCondition, possibleInt);
        }

        public async Task<int> UpdatePossibleAsync(TumourConditionDisPlay eidtorFilter)
        {
            TumourServices tumourService = new TumourServices();
            return await tumourService.UpdatePossibleAsync(eidtorFilter);
        }

        public async Task<int> UpdatePossibleItemAsync(TumourConditionDisPlay eidtorFilter )
        {
            TumourServices tumourService = new TumourServices();
            return await tumourService.UpdatePossibleItemAsync(eidtorFilter);
        }

        public async Task<int> DeletePossibleItemAsync(TumourConditionDisPlay eidtorFilter)
        {
            TumourServices tumourService = new TumourServices();
            return await tumourService.DeletePossibleItemAsync(eidtorFilter);
        }
    }
}
